/**
 * @Company 航天科技山西公司
 * @Project hitech-WORKFLOW-Web
 * @Package com.hitech.web.workflow
 * @Title ActModelController.java
 * @Description TODO(描述)
 * @author 颜金星
 * @create 2017年4月21日-下午2:52:29
 * @version V 1.0
 */
package com.casic.modules.activiti.controller;

import com.casic.common.utils.PageUtils;
import com.casic.common.utils.Query;
import com.casic.common.utils.R;
import com.casic.modules.activiti.service.ActModelService;
import com.casic.modules.activiti.util.JqGridPageView;
import com.casic.modules.activiti.util.WebUtil;
import com.casic.modules.activiti.vo.ModelVo;
import com.casic.modules.sys.controller.AbstractController;
import com.fasterxml.jackson.core.JsonProcessingException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.activiti.engine.repository.Model;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Company 航天科技山西公司
 * @Project hitech-WORKFLOW-Web
 * @Package com.hitech.web.workflow
 * @ClassName ActModelController.java
 * @Description TODO(工作流模型控制类)
 * @author 颜金星
 * @create 2017年4月21日-下午2:52:29
 */
@RestController
@Api(value = "工作流模型", description = "工作流模型")
@RequestMapping(value = "/workflow/actModel", method = RequestMethod.POST)
public class ActModelController extends AbstractController {
      
	  @Autowired
	  private ActModelService actModelService;
	  /**
	   * 
	   * @Title getActModelList
	   * @Description (流程模型列表)
	   * @author 颜金星
	   * @create 2017年4月21日-下午5:18:10
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "流程模型列表")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/getActModelList",method = RequestMethod.GET)
	  public R getActModelList(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) {

	  	Map<String, Object> params = WebUtil.getParameterMap(request);
		  int firstResult = 1;
		  int maxResults = 10;
		  if(params.get("page")!=null){
			  firstResult=Integer.parseInt((String)params.get("page"));
		  }
		  if(params.get("limit")!=null){
			  maxResults = Integer.parseInt((String)params.get("limit"));
		  }
		  if(!params.containsKey("rows")){
		  	params.put("rows",Integer.parseInt((String)params.get("limit")));
		  }
		    JqGridPageView<Model> page=actModelService.getActModelList(params);
		  int pagetotal=(int)page.getRecords();

		  PageUtils pageUtil = new PageUtils(page.getRows(), pagetotal, maxResults, Integer.parseInt(params.get("page")+""));

		  return R.ok().put("page", pageUtil);


	  }
	  /**
	   * 
	   * @Title createActModel
	   * @Description (创建流程模型)
	   * @author 颜金星
	   * @create 2017年4月21日-下午5:57:44
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @Param @throws UnsupportedEncodingException
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "创建流程模型")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/createActModel")
	  public R createActModel(HttpServletRequest request, @RequestBody ModelVo modelVo, HttpServletResponse response) throws UnsupportedEncodingException {
		    Map<String, Object> params = new HashMap<String, Object>();
		    params.put("name",modelVo.getName());
		    params.put("description",modelVo.getDescription());
		    Model model=actModelService.createActModel(params);
		  params.put("model",model);
		  return R.ok();
	  }
	  /**
	   * 
	   * @Title deployActModel
	   * @Description (部署流程模型)
	   * @author 颜金星
	   * @create 2017年4月21日-下午6:26:31
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @Param @throws JsonProcessingException
	   * @Param @throws IOException
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "部署流程模型")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/deployActModel",method = RequestMethod.GET)
	  public R deployActModel(HttpServletRequest request, String id, HttpServletResponse response) throws JsonProcessingException, IOException {
		    Map<String, String> msg=actModelService.deployActModel(id);
		    if("success".equals(msg.get("state"))){
				return R.ok();
		    }else{
				return R.error();
		    }
	  }
	  /**
	   * 
	   * @Title exportActModel
	   * @Description (下载流程模型)
	   * @author 颜金星
	   * @create 2017年4月21日-下午7:02:30
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @Param @throws IOException
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "下载流程模型")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/exportActModel" ,method = RequestMethod.GET)
	  public void exportActModel(HttpServletRequest request, ModelMap modelMap, HttpServletResponse response) throws IOException {
		    Map<String, Object> params = WebUtil.getParameterMap(request);
		    String id=(String)params.get("id");
		    actModelService.exportActModel(id,response);
	  }
	  /**
	   * 
	   * @Title deleteModel
	   * @Description (删除流程模型)
	   * @author 颜金星
	   * @create 2017年5月26日-下午5:17:08
	   * @Param @param request
	   * @Param @param modelMap
	   * @Param @param response
	   * @Param @return
	   * @Param @throws UnsupportedEncodingException
	   * @return Object
	   * @throws
	   */
	  @ApiOperation(value = "删除流程模型")
	  //@RequiresPermissions("sys.dept.read")
      @RequestMapping(value = "/deleteModel/{id}")
	  public R deleteModel(HttpServletRequest request, @PathVariable("id") String id, HttpServletResponse response) throws UnsupportedEncodingException {
		    Map<String, Object> params =new  HashMap<String, Object> ();
		    params.put("id",id);
		    actModelService.deleteModel(params);
		  return R.ok();
	  }
}
